package com.lspopi.web.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lspopi.entity.Article;
import com.lspopi.service.ArticleServiceI;
import com.lspopi.web.common.Customer;

@Controller
public class Index {
	private static final Logger logger = LoggerFactory.getLogger(Index.class);
	@Autowired
	private ArticleServiceI articleService;
	private String PREFIX = "Elliot/";

	@RequestMapping({ "/index", "/" })
	public String index(ModelMap map) {
		return PREFIX + "index";
	}

	@RequestMapping({ "/gallery", "/gallery.html" })
	public String gallery(ModelMap map) {
		return PREFIX + "gallery";
	}

	@RequestMapping({ "/page", "/page.html" })
	public String page(ModelMap map) {
		return PREFIX + "page";
	}

	@RequestMapping({ "/page-fullwidth", "/page-fullwidth.html" })
	public String pageFullwidth(ModelMap map) {
		return PREFIX + "page-fullwidth";
	}

	@RequestMapping({ "/page-wide", "/page-wide.html" })
	public String pageWide(ModelMap map) {
		return PREFIX + "page-wide";
	}

	@RequestMapping({ "/page-fixed-footer", "/page-fixed-footer.html" })
	public String pageFixedFooter(ModelMap map) {
		return PREFIX + "page-fixed-footer";
	}

	@RequestMapping({ "/index-fixed-footer", "/index-fixed-footer.html" })
	public String indexFixedFooter(ModelMap map) {
		return PREFIX + "index-fixed-footer";
	}

	@RequestMapping({ "/blog{page}", "/blog{page}.html" })
	public String blog(@PathVariable(value = "page") Integer page, ModelMap map) {
		if (page == null) {
			page = 1;
		}
		logger.debug("the current page is {}", page);

		PageHelper.startPage(page, Customer.PAGE_SIZE);
		List<Article> articles = articleService.selectArticles(null);
		logger.debug("{}", articles);
		PageInfo<Article> pageInfo = new PageInfo<Article>(articles);
		logger.debug("{}", pageInfo);
		map.put("pageInfo", pageInfo);
		return PREFIX + "blog";
	}

	@RequestMapping({ "/contact", "/contact.html" })
	public String contact(ModelMap map) {
		return PREFIX + "contact";
	}

	@RequestMapping({ "/single/{id}", "/single/{id}.html" })
	public String single(@PathVariable(value = "id") Long id, ModelMap map) {
		Article article = articleService.selectByPrimaryKey(id);
		map.put("article", article);
		return PREFIX + "single";
	}

}
